<template>
	<!-- 优惠券列表 -->
	<div class="content"  v-if="!is_edit">
		<div class="content-top">
			优惠券管理_优惠券查询
		</div>
		
		<div class="content-search">
			<el-form :inline="true" class="demo-form-inline" style="margin-top: 20px;">
				<el-form-item label="优惠券名称：">
					<el-input placeholder="优惠券名称" v-model="name"></el-input>
				</el-form-item>
               <el-form-item label="状态：">
				   <el-select placeholder="请选择" v-model="state">
						<el-option
						v-for="item in couponType"
						:key="item.id"
						:label="item.name"
						:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="时间范围：">
					 <el-date-picker
						v-model="time"
						type="datetimerange"
						range-separator="至"
						start-placeholder="开始日期"
						end-placeholder="结束日期"
						value-format="yyyy-MM-dd HH:mm:ss">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="onSearch" icon="el-icon-search">搜索</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="content-operation">
			<el-button type="primary" icon="el-icon-plus" @click='is_add = true' v-if="has_add">添加优惠券</el-button>
		</div>
		<div class="content-table">
			<el-table :data="tableData" show-header stripe style="width: 100%;">
				<el-table-column prop="name" align="center" label="优惠券名称"  width="120">
				</el-table-column>
				<el-table-column prop="code" align="center" label="优惠券编码"  width="150">
				</el-table-column>
				<el-table-column prop="publishCount" align="center" label="发行数量">
				</el-table-column>
				<el-table-column prop="receiveCount" align="center" label="已领取数量" width="120">
				</el-table-column>
				<el-table-column prop="useCount" align="center" label="已使用数量" width="120">
				</el-table-column>
				<el-table-column align="center" label="面额">
					<template slot-scope="scope">
						{{scope.row.amount +'元'}}
					</template>
				</el-table-column>
				<el-table-column align="center" label="使用条件">
					<template slot-scope="scope">
						{{scope.row.minPoint == 0?'无限制':'满'+scope.row.minPoint+'元'}}
					</template>
				</el-table-column>
				<el-table-column prop="startTime" align="center" label="生效时间" width="160">
				</el-table-column>
				<el-table-column prop="endTime" align="center" label="失效时间" width="160">
				</el-table-column>
				<el-table-column align="center" label="状态">
					<template slot-scope="scope">
						{{scope.row.type}}
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" width="140">
					<template slot-scope="scope">
						<el-button @click="handleClick(scope.row)" v-if="has_edit" type="text" size="small"><i class="el-icon-edit"></i>修改</el-button>
						<el-button @click="handleClick1(scope.row)" v-if="has_del" type="text" size="small"><i class="el-icon-delete"></i>删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<div class="content-page">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="page"
			 :page-size=limit layout="prev, pager, next, jumper" :total=total>
			</el-pagination>
		</div>
		<template v-if="is_add">
			<div class="all-screen"></div>
			<div class='add-box'>
				<CouponAdd @saveAdd='saveAdd'></CouponAdd>
			</div>
		</template>
		
	</div>
	<CouponDetail v-else @saveEdit='saveEdit' :id='id'></CouponDetail>
</template>

<script>
	import CouponAdd from '@/components/coupon/CouponAdd.vue'
	import CouponDetail from '@/components/coupon/CouponDetail.vue'
	import Global from '@/components/Global.vue'
	export default {
		components: {
			CouponAdd: CouponAdd,
			CouponDetail: CouponDetail,
		},
		props:['power'],
		data() {
			return {
				id: 'id',
				is_add: false,
				is_edit: false,
				tableData: [],
				limit: 10,
				page: 1,
				total: 1,
				has_add:false,
				has_edit:false,
				has_del:false,
				couponType:[{id:0,name:'0'},{id:1,name:'1'},{id:2,name:'2'},{id:3,name:'3'}],
				name:'',
				state:'',
				time:''
			}
		},
		mounted() {
			this.check_power();
			this.get_list();
		},
		methods: {
			onSearch(){
				this.page = 1
				this.get_list()
			},
			textConversion(idx){
				for(let item of this.couponType){
					if(item.id == idx){
                      return item.name
					}
				}
			},
			check_power(){
				var self = this;
				this.power.forEach(function(element) {
					if(element.permissionName.indexOf("添加") != -1 )self.has_add = true;
					if(element.permissionName.indexOf("编辑") != -1 )self.has_edit = true;
					if(element.permissionName.indexOf("删除") != -1 )self.has_del = true;
				});
			},
			get_list() {
				var self = this;
				var start = ''
				var end = ''
				console.log(self.time)
				if(self.time != null){
					start = self.time[0]
					end = self.time[1]
				}
				this.$axios.post(this.$global.serverIp + 'frontend/coupon/findPage', {
				 "limit": self.limit + "",
				 "page": self.page + "",
				 "name":self.name,
				 "type":self.state,
				 "startTime":start,
				 "endTime":end
				})
				  .then(function (res) {
					  if(res != ''){
						  self.tableData = res.data.list;
						  self.total = res.data.totalCount;
						  self.page = res.data.currPage;
					  }
						
				  })
			},
			saveAdd(type) {
				if (type == 2) {
					this.get_list();
				}
				this.is_add = false;
			},
			
			saveEdit(type) {
				if (type == 2) {
					this.get_list();
				}
				this.is_edit = false;
			},
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
			},
			handleCurrentChange(val) {
				this.page = val;
				this.get_list();
				console.log(`当前页: ${val}`);
			},
			handleClick(row) {
				console.log(row.name);
				this.id = row.id;
				this.is_edit = true;
			},
			handleClick1(row) {
				this.delopen(row);
			},
			delopen(row) {
				var self = this;
				this.$confirm('此操作将删除该优惠券, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					$.ajax({
						url: Global.serverIp + 'jianeng-frontend/coupon/delete',
						data: '["' + row.id + '"]',
						xhrFields: {
							withCredentials: true
						},
						beforeSend : function(request) {
							request.setRequestHeader("Authorization", 'Bearer '+localStorage.getItem('token'));
						},
						contentType: 'application/json;charset=utf-8',
						dataType: 'json',
						type: 'delete',
						success: function(res) {
							if (res.code == Global.success_code) {
								console.log(res);
								self.$popup.open("删除成功");
								self.get_list();
							} else {
								self.$popup.open(res.msg);
							}
						},
						error: function(res) {
							if(res.responseJSON.code == '040005'){
								self.$popup.open("登录过期");
							}else{
								self.$popup.open("请求失败");
							}
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			
		},

	}
	import '@/assets/css/common.css'
</script>
